// 全局变量
let scene, camera, renderer;
let gameObjects = {};

function init() {
    // 初始化场景
    SceneManager.init();

    // 获取场景组件
    scene = SceneManager.scene;
    camera = SceneManager.camera;
    renderer = SceneManager.renderer;

    // 初始化灯光
    LightManager.init(scene);

    // 创建游戏对象
    ObjectCreator.init(scene);
    gameObjects = ObjectCreator.getObjects();

    // 设置控制
    ControlManager.init(camera, renderer, LightManager, AudioManager);

    // 初始化音频
    AudioManager.init();

    // 开始渲染循环
    animate();
}

function animate() {
    requestAnimationFrame(animate);

    // 添加轻微的人物摆动动画
    if (gameObjects.person) {
        gameObjects.person.rotation.y = Math.sin(Date.now() * 0.0005) * 0.02;
    }

    // 手电筒人物轻微摆动
    if (gameObjects.flashlightPerson) {
        gameObjects.flashlightPerson.rotation.y = Math.sin(Date.now() * 0.0003) * 0.01;
    }

    renderer.render(scene, camera);
}

function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
}

window.addEventListener('resize', onWindowResize);

// 初始化游戏
init();